package com.second.app.thread.reentrant.conditionmoreerror;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gyz
 * @date 2021/2/1 16:27
 */
public class MyService {

    private final ReentrantLock reentrantLock = new ReentrantLock();

    public Condition condition = reentrantLock.newCondition();

    public void awaitA() {
        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " :开始等待" + System.currentTimeMillis());
            condition.await();
            System.out.println(Thread.currentThread().getName() + " :结束等待" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }

    public void awaitB() {
        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " :开始等待" + System.currentTimeMillis());
            condition.await();
            System.out.println(Thread.currentThread().getName() + " :结束等待" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
        }
    }

    public void signalAll() {
        reentrantLock.lock();
        try {
            condition.signalAll();
            System.out.println(Thread.currentThread().getName() + "：唤醒时间" + System.currentTimeMillis());
        } finally {
            reentrantLock.unlock();
        }
    }

}
